package com.lv.utils.service.impl;

import com.lv.utils.mapper.CronMysqlMapper;
import com.lv.utils.pojo.Cron;
import com.lv.utils.restartTask.ConfigTask;
import com.lv.utils.service.CronService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: Lv_sc
 * Date:     2021/3/29 14:10
 * Description: 描述
 */
@Service
public class CronServiceImpl implements CronService {

    @Autowired
    ConfigTask configTask;

    @Autowired
    private CronMysqlMapper cronMapper;

    @Override
    public int insertCron(Cron cron) {
        //默认添加为开启状态
        cron.setStatus(1);
        cronMapper.stopAllCron();
        int i = cronMapper.insertCron(cron);
        return i;
    }

    @Override
    public int updateCronStatus(Cron cron) {
        int result = -1;
        int status = cron.getStatus();
        //开启
        if(status==1){
            //停止所有其他的
            cronMapper.stopAllCron();
            //开启当前
            result = cronMapper.updateStatus(cron);
            //重启任务
            configTask.restart();
        }else {
            //关闭当前
            result = cronMapper.updateStatus(cron);
            configTask.stop();
        }
        return result;
    }
}